//
//  YXJCDetailEChartsView.m
//  Tunnel
//
//  Created by wbb on 2025/6/15.
//

#import "YXJCDetailEChartsView.h"
#import "HyCategories.h"
#import "HyChartsLineDemoCursor.h"
#import "HyCharts.h"

@implementation YXJCDetailEChartsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        
    }
    return self;
}
- (void)viewWithData:(id)data {
    [self setDataSource:@[] titles:@[]];
}
 - (void)setDataSource:(NSArray *)values titles:(NSArray *)titles {
    _titles = titles;
    _values = values;
    
     for (UIView * view in self.subviews) {
         [view removeFromSuperview];
     }
    UIView *oneChartView =  [self oneChartLinViewWithFrame:CGRectMake(15, 0, kSCREEN_WIDTH -30 - 24, ScaleNum(150))];
    [self addSubview:oneChartView];
}
- (UIView *)oneChartLinViewWithFrame:(CGRect)frame {
    
    UIView *contentView = [[UIView alloc] initWithFrame:frame];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.layer.cornerRadius = 5;
    contentView.layer.borderWidth = 1.0;
    contentView.layer.borderColor = UIColor.groupTableViewBackgroundColor.CGColor;
    contentView.clipsToBounds = NO;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, CGRectGetWidth(frame) - 20, 25)];
    titleLabel.text = @"(ppm)";
    titleLabel.font = [UIFont systemFontOfSize:10];
    titleLabel.textColor = main_light_text_color_9;
    [contentView addSubview:titleLabel];
    
    HyChartLineView *chartLineView =  [[HyChartLineView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame) + 15, CGRectGetWidth(titleLabel.frame), CGRectGetHeight(frame) - CGRectGetMaxY(titleLabel.frame) - 25)];
    [contentView addSubview:chartLineView];
    
    chartLineView.contentEdgeInsets = UIEdgeInsetsMake(0, 35, 35, 10);
    chartLineView.pinchGestureDisabled = YES;
    
    // 自定义游标
    HyChartsLineDemoCursor *cursor = HyChartsLineDemoCursor.new;
    cursor.showView = chartLineView;
    [chartLineView resetChartCursor:cursor];
    kWEAK_SELF;
    // 配置坐标轴
    [[chartLineView.dataSource.axisDataSource configXAxisWithModel:^(id<HyChartXAxisModelProtocol>  _Nonnull xAxisModel) {
        xAxisModel.topXaxisDisabled = NO;
        [[[xAxisModel configNumberOfIndexs:10] configBottomXAxisInfo:^(id<HyChartXAxisInfoProtocol>  _Nonnull xAxisInfo) {
            xAxisInfo.rotateAngle = - M_PI_4;
            xAxisInfo.axisTextFont = [UIFont systemFontOfSize:10];
            xAxisInfo.displayAxisZeroText = NO;
        }] configTopXAxisInfo:^(id<HyChartXAxisInfoProtocol>  _Nonnull xAxisInfo) {
            xAxisInfo.autoSetText = NO;
            xAxisInfo.axisLineWidth = 1;
            xAxisInfo.axisLineColor = UIColor.groupTableViewBackgroundColor;
        }];
    }] configYAxisWithModel:^(id<HyChartYAxisModelProtocol>  _Nonnull yAxisModel) {
        [[[[yAxisModel configNumberOfIndexs:5] configYAxisMaxValue:^NSNumber * _Nonnull{
            CGFloat maxNum = [[self->_values valueForKeyPath:@"@max.integerValue"] integerValue];
            return @(maxNum);
        }] configLeftYAxisInfo:^(id<HyChartYAxisInfoProtocol>  _Nonnull yAxisInfo) {
            yAxisInfo.axisTextFont = [UIFont systemFontOfSize:10];
            yAxisInfo.axisTextPosition = HyChartAxisTextPositionBinus;
            yAxisInfo.axisLineType = HyChartAxisLineTypeNone;
            yAxisInfo.displayAxisZeroText = NO;
        }] configYAxisMinValue:^NSNumber * _Nonnull{
            return  @(0);
        }];
    }];
    
    // 配置图表信息
    [chartLineView.dataSource.configreDataSource configConfigure:^(id<HyChartLineConfigureProtocol>  _Nonnull configure) {
        configure.width = 0;
        configure.autoMargin = YES;
        [configure configLineConfigureAtIndex:^(NSInteger index, id<HyChartLineOneConfigureProtocol>  _Nonnull oneConfigure) {
            
            oneConfigure.lineWidth = 2;
            oneConfigure.lineColor = [UIColor colorWithRed:50/255.0 green:175/255.0 blue:255/255.0 alpha:1];
            oneConfigure.lineType = HyChartLineTypeCurve;
            
            oneConfigure.linePointWidth = .5;
            oneConfigure.linePointSize = CGSizeMake(5, 5);
            oneConfigure.linePointType = HyChartLinePointTypeRing;
            oneConfigure.linePointFillColor = UIColor.whiteColor;
            oneConfigure.shadeColors = @[[UIColor colorWithRed:50/255.0 green:175/255.0 blue:255/255.0 alpha:.8], [UIColor colorWithRed:50/255.0 green:175/255.0 blue:255/255.0 alpha:.6], [UIColor colorWithRed:50/255.0 green:175/255.0 blue:255/255.0 alpha:.4] ,[UIColor colorWithRed:50/255.0 green:175/255.0 blue:255/255.0 alpha:.2], [UIColor colorWithRed:50/255.0 green:175/255.0 blue:255/255.0 alpha:.1]];
        }];
    }];
    
    NSArray<NSString *> *titles = @[@"03-08", @"03-09", @"03-10", @"03-11", @"03-12", @"03-13", @"03-14", @"03-18", @"03-19"];
    if(_titles.count > 0){
        titles = _titles;
    }
    NSMutableArray<NSNumber *> *values = @[].mutableCopy;
    if(_values.count == 0){
        [titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [values addObject:@((int)(arc4random() % 250))];
        }];
    }else{
        values = _values;
    }

    
    // 配置数据
    [[chartLineView.dataSource.modelDataSource configNumberOfItems:^NSInteger{
        return values.count;
    }] configModelForItemAtIndex:^(id<HyChartLineModelProtocol>  _Nonnull model, NSInteger index) {
        model.text = titles[index];
        model.values = @[@([values[index] integerValue])];
        // 自定义数据
        NSDictionary *dict = @{@"colors" : @[[UIColor colorWithRed:50/255.0 green:175/255.0 blue:255/255.0 alpha:.8]]};
        model.exData = dict;
    }];
    

    [chartLineView setNeedsRendering];
    
    return contentView;
}
@end
